package com.wendy.read.member.service.Impl;

import com.wendy.read.member.mapper.UserMapper;
import com.wendy.read.member.pojo.User;
import com.wendy.read.member.service.UserService;
import com.wendy.read.member.utils.MD5Util;
import com.wendy.read.member.utils.ThreadLocalUtil;
//import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public void register(String username, String password) {
        userMapper.add(username, MD5Util.getMD5String(password));
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        userMapper.updateAvatar(avatarUrl, (Integer) map.get("id"));
    }

    @Override
    public void updatePwd(String newPwd, @NotNull Integer id) {
        userMapper.updatePwd(MD5Util.getMD5String(newPwd), id);
    }
}
